#!/usr/bin/env python
"""
html utilities
"""

__author__ = 'rx.wen218@gmail.com'

html_encoding_table = {
        " ": "&nbsp;",
        "<": "&lt;",
        ">": "&gt;",
        '"': "&quot;"
        }

def EncodeHtml(src):
    # & needs to be processed first
    # the order of dictionary elements being iterated is undefined
    encode = src.replace("&", "&amp;")
    for (key, value) in html_encoding_table.items():
        encode = encode.replace(key, value)
    return encode

def DecodeHtml(src):
    decode = src
    for (key, value) in html_encoding_table.items():
        decode = decode.replace(value, key)
    # it has to be done last
    decode = decode.replace("&amp;", "&")
    return decode 
